<?php

namespace App\Http\Controllers\Common;

use Illuminate\Http\Request;
use App\Models\System\Module;
use App\Http\Controllers\Controller;
use App\Repositories\CompanyRepository;
use Illuminate\Database\Eloquent\Builder;

/**
 * 公共数据API
 *
 * Date: 27/04/2018
 * @author George
 * @package App\Http\Controllers\Common
 */
class PublicController extends Controller
{
	protected $companyRepository;

	/**
	 * PublicController constructor.
	 * @param $companyRepository
	 */
	public function __construct(CompanyRepository $companyRepository)
	{
		$this->companyRepository = $companyRepository;
	}

	/**
	 * 获取公司列表信息
	 *
	 * Date: 27/04/2018
	 * @author George
	 * @param Request $request
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function companies(Request $request)
	{
		$city = $request->get('city');
		$companies = $this->companyRepository->getByCity($city);
		return success($companies);
	}

	/**
	 * 获取模块
	 *
	 * Date: 28/04/2018
	 * @author George
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function modules(Request $request)
	{
		$name = $request->get('name');
		$query = Module::query();
		$query->where('status', 1);
		$query->when($name, function ($name, $query) {
			/**
			 * @var Builder $query
			 */
			return $query->where('name', 'like', "%{$name}%");
		});
		$modules = $query->get();
		return success($modules);
	}
}
